@extends('layouts.admin')

@section('title', '岗位管理-权限管理')

@section('style')
    <link rel="stylesheet" href="{{asset('lib/zTree/v3/css/zTreeStyle/zTreeStyle.css')}}" type="text/css">
@endsection

@section('content')
    <article class="page-container">
        <div class="row cl">
            <div id="station_permissions_tree" class="ztree"></div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <form action="" method="post" class="form form-horizontal" id="form-techValue-edit">
                    {{--<input type="hidden" id="role_id" name="role_id" value="{{$role_id}}">--}}
                    {{--<input type="hidden" id="permissions" name="permissions[]" value="">--}}
                    <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                </form>
            </div>
        </div>
    </article>
@endsection
@section('script')
    <script type="text/javascript" src="{{asset('lib/zTree/v3/js/jquery.ztree.all-3.5.min.js')}}"></script>
    <script>
        $(document).ready(function() {
            $("#form-techValue-edit").validate({
                submitHandler: function(form){
                    var ids = [], nodes = tree.getCheckedNodes(true);
                    for(var i=0; i<nodes.length; i++) {
                        ids.push(nodes[i].id);
                    }
                    $(form).ajaxSubmit({
                        type: 'post',
                        url: "{{url('personnel/station/permissionEdit')}}",
                        data:{'role_id':'{{$role_id}}','permissions':ids},
                        success: function(data){
                            if(data['successFlag']==1){
                                layer_success_message("操作成功!");
                                setTimeout(function(){
                                    layer_close();
                                },500);
                            }else{
                                layer_error_message(data['msg']);
                            }
                        },
                        error: function(XmlHttpRequest, textStatus, errorThrown){
                            layer_error_message('操作失败!');
                        }
                    });
                },
                errorContainer: "#messageBox",
                errorPlacement: function(error, element) {
                    $("#messageBox").text("输入有误，请先更正。");
                    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });

            var setting = {check:{enable:true,nocheckInherit:true},view:{selectedMulti:false},
                data:{simpleData:{enable:true}},callback:{beforeClick:function(id, node){
                    tree.checkNode(node, !node.checked, true, true);
                    return false;
                }}};

            // 用户-菜单
            var zNodes=[
                @foreach($allPermissions as $permission)
                {id:"{{$permission->id}}", pId:"{{$permission->parent_id}}", name:"{{$permission->display_name}}"},
                @endforeach
            ];

            // 初始化树结构
            var tree = $.fn.zTree.init($("#station_permissions_tree"), setting, zNodes);
            // 不选择父节点
            tree.setting.check.chkboxType = { "Y" : "ps", "N" : "s" };
            // 默认选择节点
            var ids = [
                 @foreach($rolePermissions as $permission)
                "{{$permission->id}}",
                @endforeach
            ]
            for(var i=0; i<ids.length; i++) {
                var node = tree.getNodeByParam("id", ids[i]);
                try{tree.checkNode(node, true, false);}catch(e){}
            }
            // 默认展开全部节点
            tree.expandAll(true);
        });
    </script>
@endsection